$(function() {
	var groupId = -1;

	//echarts
	// filter Time
	var rangesConf = {};
	rangesConf[I18n.daterangepicker_ranges_today] = [moment().startOf('day'), moment().endOf('day')];
	rangesConf[I18n.daterangepicker_ranges_yesterday] = [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days').endOf('day')];
	rangesConf[I18n.daterangepicker_ranges_this_month] = [moment().startOf('month'), moment().endOf('month')];
	rangesConf[I18n.daterangepicker_ranges_last_month] = [moment().subtract(1, 'months').startOf('month'), moment().subtract(1, 'months').endOf('month')];
	rangesConf[I18n.daterangepicker_ranges_recent_week] = [moment().subtract(1, 'weeks').startOf('day'), moment().endOf('day')];
	rangesConf[I18n.daterangepicker_ranges_recent_month] = [moment().subtract(1, 'months').startOf('day'), moment().endOf('day')];

	$('#filterTime').daterangepicker({
		autoApply:false,
		singleDatePicker:false,
		showDropdowns:false,        // 是否显示年月选择条件
		timePicker: true, 			// 是否显示小时和分钟选择条件
		timePickerIncrement: 10, 	// 时间的增量，单位为分钟
		timePicker24Hour : true,
		opens : 'left', //日期选择框的弹出位置
		ranges: rangesConf,
		locale : {
			format: 'YYYY-MM-DD HH:mm:ss',
			separator : ' - ',
			customRangeLabel : I18n.daterangepicker_custom_name ,
			applyLabel : I18n.system_ok ,
			cancelLabel : I18n.system_cancel ,
			fromLabel : I18n.daterangepicker_custom_starttime ,
			toLabel : I18n.daterangepicker_custom_endtime ,
			daysOfWeek : I18n.daterangepicker_custom_daysofweek.split(',') ,        // '日', '一', '二', '三', '四', '五', '六'
			monthNames : I18n.daterangepicker_custom_monthnames.split(',') ,        // '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'
			firstDay : 1
		},
		startDate: rangesConf[I18n.daterangepicker_ranges_recent_week][0] ,
		endDate: rangesConf[I18n.daterangepicker_ranges_recent_week][1]
	}, function (start, end, label) {
		freshChartDate(start, end);
	});
	freshChartDate(rangesConf[I18n.daterangepicker_ranges_recent_week][0], rangesConf[I18n.daterangepicker_ranges_recent_week][1]);

	function freshChartDate(startDate, endDate) {
		$.ajax({
			type : 'POST',
			url : base_url + '/jobinfo/chartInfo',
			data : {
				'startDate':startDate.format('YYYY-MM-DD HH:mm:ss'),
				'endDate':endDate.format('YYYY-MM-DD HH:mm:ss')
			},
			dataType : "json",
			success : function(data){
				if (data.code == 200) {

					pieChartInit(data);
				} else {
					layer.open({
						title: I18n.system_tips ,
						btn: [ I18n.system_ok ],
						content: (data.msg || I18n.job_dashboard_report_loaddata_fail ),
						icon: '2'
					});
				}
			}
		});
	}

	/**
	 * line Chart Init
	 */
	function lineChartInit(data) {
		var option = {
			title: {
				text: I18n.job_dashboard_date_report
			},
			tooltip : {
				trigger: 'item',
				formatter: "{b} : {c} ({d}%)"
			},
			legend: {
				data:[I18n.joblog_status_suc, I18n.joblog_status_fail, I18n.joblog_status_running]
			},
			toolbox: {
				feature: {
					/*saveAsImage: {}*/
				}
			},
			grid: {
				left: '3%',
				right: '4%',
				bottom: '3%',
				containLabel: true
			},
			xAxis : [
				{
					type : 'category',
					boundaryGap : false,
					data : data.content.triggerDayList
				}
			],
			yAxis : [
				{
					type : 'value'
				}
			],
			series : [
				{
					name:I18n.joblog_status_suc,
					type:'line',
					stack: 'Total',
					areaStyle: {normal: {}},
					data: data.content.triggerDayCountSucList
				},
				{
					name:I18n.joblog_status_fail,
					type:'line',
					stack: 'Total',
					label: {
						normal: {
							show: true,
							position: 'top'
						}
					},
					areaStyle: {normal: {}},
					data: data.content.triggerDayCountFailList
				},
				{
					name:I18n.joblog_status_running,
					type:'line',
					stack: 'Total',
					areaStyle: {normal: {}},
					data: data.content.triggerDayCountRunningList
				}
			],
			color:['#00A65A', '#c23632', '#F39C12']
		};

		var lineChart = echarts.init(document.getElementById('lineChart'));
		lineChart.setOption(option);
	}

	/**
	 * pie Chart Init
	 */
	function pieChartInit(data) {
		var option = {
			title : {
				text: I18n.job_dashboard_rate_report ,
				/*subtext: 'subtext',*/
				x:'center'
			},
			tooltip : {
				trigger: 'axis',
				axisPointer: {
					type: 'cross',
					label: {
						backgroundColor: '#586773'
					}
				}
			},
			legend: {
				orient: 'vertical',
				right: 'right',
				data: [I18n.joblog_status_suc, I18n.joblog_status_fail, I18n.joblog_status_running ]
			},
			series : [
				{
					//name: '分布比例',
					type: 'pie',
					radius : '55%',
					center: ['50%', '60%'],
					data:[
						{
							name:I18n.joblog_status_suc,
							value:data.content.triggerCountSucTotal
						},
						{
							name:I18n.joblog_status_fail,
							value:data.content.triggerCountFailTotal
						},
						{
							name:I18n.joblog_status_running,
							value:data.content.triggerCountRunningTotal
						}
					],
					itemStyle: {
						emphasis: {
							shadowBlur: 10,
							shadowOffsetX: 0,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					}
				}
			],
			color:['#09a4a6', '#e19bf3','#c2a61a' ]
		};
		var pieChart = echarts.init(document.getElementById('pieChart'));
		pieChart.setOption(option);
	}


	/**
	 * fresh Chart Date
	 *
	 * @param startDate
	 * @param endDate
	 */


	// init date tables
	var jobTable = $("#job_list").dataTable({
		"deferRender": true,
		"processing" : true,
		"serverSide": true,
		"ajax": {
			url: base_url + "/jobinfo/pageList",
			type:"post",
			data : function ( d ) {
				var obj = {};
				obj.jobGroup = $('#jobGroup').val();
				obj.triggerStatus = $('#triggerStatus').val();
				obj.jobDesc = $('#jobDesc').val();
				obj.executorHandler = $('#executorHandler').val();
				obj.author = $('#author').val();
				obj.start = d.start;
				obj.length = d.length;

				groupId = $('#jobGroup').val();
				console.log(groupId);
				return obj;
			}

		},
		"searching": false,
		"ordering": false,
		//"scrollX": true,	// scroll x，close self-adaption
		"columns": [
			{
				"data": 'id',
				"bSortable": false,
				"visible" : true,
				"width":'7%'
			},
			{
				"data": 'jobGroup',
				"visible" : false,
				"render": function ( data, type, row ) {
					var groupMenu = $("#jobGroup").find("option");
					for ( var index in $("#jobGroup").find("option")) {
						if ($(groupMenu[index]).attr('value') == data) {
							return $(groupMenu[index]).html();
						}
					}

					return data;
				}
			},
			{
				"data": 'jobDesc',
				"visible" : true,
				"width":'25%'
			},
			//资源类型
			{
				"data": 'sourceType',
				"visible" : true

				// "visible": true
			},
			{ "data": 'executorParam', "visible" : false},
			{
				"data": 'addTime',
				"visible" : false,
				"render": function ( data, type, row ) {
					return data?moment(new Date(data)).format("YYYY-MM-DD HH:mm:ss"):"";
				}
			},
			{
				"data": 'updateTime',
				"visible" : false,
				"render": function ( data, type, row ) {
					return data?moment(new Date(data)).format("YYYY-MM-DD HH:mm:ss"):"";
				}
			},
			{ "data": 'author', "visible" : true, "width":'10%'},
			{ "data": 'alarmEmail', "visible" : false},
			{
				"data": 'triggerStatus',
				"width":'10%',
				"visible" : false,
				"render": function ( data, type, row ) {
					// status
					if (1 == data) {
						return '<small class="label label-success" >RUNNING</small>';
					} else {
						return '<small class="label label-default" >STOP</small>';
					}
					return data;
				}
			},
			{
				"data": I18n.system_opt ,
				"width":'10%',
				"render": function ( data, type, row ) {
					return function(){

						// status
						var start_stop_div = "";
						if (1 == row.triggerStatus ) {
							start_stop_div = '<li><a href="javascript:void(0);" class="job_operate" _type="job_pause" >'+ I18n.jobinfo_opt_stop +'</a></li>\n';
						} else {
							start_stop_div = '<li><a href="javascript:void(0);" class="job_operate" _type="job_resume" >'+ I18n.jobinfo_opt_start +'</a></li>\n';
						}

						// job_next_time_html
						var job_next_time_html = '';
						if (row.scheduleType == 'CRON' || row.scheduleType == 'FIX_RATE') {
							job_next_time_html = '<li><a href="javascript:void(0);" class="job_next_time" >' + I18n.jobinfo_opt_next_time + '</a></li>\n';
						}

						// log url
						var logHref = base_url +'/joblog?jobId='+ row.id;

						// code url
						var codeBtn = "";
						if ('BEAN' != row.glueType) {
							var codeUrl = base_url +'/jobcode?jobId='+ row.id;
							codeBtn = '<li><a href="'+ codeUrl +'" target="_blank" >GLUE IDE</a></li>\n';
							codeBtn += '<li class="divider"></li>\n';
						}

						// data
						tableData['key'+row.id] = row;

						// opt
						var html = '<div class="btn-group">\n' +
							'     <button type="button" class="btn btn-primary btn-sm">'+ I18n.system_opt +'</button>\n' +
							'     <button type="button" class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown">\n' +
							'       <span class="caret"></span>\n' +
							'       <span class="sr-only">Toggle Dropdown</span>\n' +
							'     </button>\n' +
							'     <ul class="dropdown-menu" role="menu" _id="'+ row.id +'" >\n' +
							'       <li><a href="javascript:void(0);" class="job_trigger" >'+ I18n.jobinfo_opt_run +'</a></li>\n' +
							'       <li><a href="'+ logHref +'">'+ I18n.jobinfo_opt_log +'</a></li>\n' +
						//	'       <li><a href="javascript:void(0);" class="job_registryinfo" >' + I18n.jobinfo_opt_registryinfo + '</a></li>\n' +
						//	job_next_time_html +
						//	'       <li class="divider"></li>\n' +
						//	codeBtn +
						//	start_stop_div +
							'       <li><a href="javascript:void(0);" class="update" >'+ I18n.system_opt_edit +'</a></li>\n' +
							'       <li><a href="javascript:void(0);" class="job_operate" _type="job_del" >'+ I18n.system_opt_del +'</a></li>\n' +
						//	'       <li><a href="javascript:void(0);" class="job_copy" >'+ I18n.system_opt_copy +'</a></li>\n' +
							'     </ul>\n' +
							'   </div>';

						return html;
					};
				}
			}
		],
		"language" : {
			"sProcessing" : I18n.dataTable_sProcessing ,
			"sLengthMenu" : I18n.dataTable_sLengthMenu ,
			"sZeroRecords" : I18n.dataTable_sZeroRecords ,
			"sInfo" : I18n.dataTable_sInfo ,
			"sInfoEmpty" : I18n.dataTable_sInfoEmpty ,
			"sInfoFiltered" : I18n.dataTable_sInfoFiltered ,
			"sInfoPostFix" : "",
			"sSearch" : I18n.dataTable_sSearch ,
			"sUrl" : "",
			"sEmptyTable" : I18n.dataTable_sEmptyTable ,
			"sLoadingRecords" : I18n.dataTable_sLoadingRecords ,
			"sInfoThousands" : ",",
			"oPaginate" : {
				"sFirst" : I18n.dataTable_sFirst ,
				"sPrevious" : I18n.dataTable_sPrevious ,
				"sNext" : I18n.dataTable_sNext ,
				"sLast" : I18n.dataTable_sLast
			},
			"oAria" : {
				"sSortAscending" : I18n.dataTable_sSortAscending ,
				"sSortDescending" : I18n.dataTable_sSortDescending
			}
		}
	});

	// table data
	var tableData = {};

	// search btn
	$('#searchBtn').on('click', function(){
		jobTable.fnDraw();
	});

	// jobGroup change
	$('#jobGroup').on('change', function(){
		//reload
		var jobGroup = $('#jobGroup').val();
		window.location.href = base_url + "/jobinfo?jobGroup=" + jobGroup;
	});

	// job operate
	$("#job_list").on('click', '.job_operate',function() {
		var typeName;
		var url;
		var needFresh = false;

		var type = $(this).attr("_type");
		if ("job_pause" == type) {
			typeName = I18n.jobinfo_opt_stop ;
			url = base_url + "/jobinfo/stop";
			needFresh = true;
		} else if ("job_resume" == type) {
			typeName = I18n.jobinfo_opt_start ;
			url = base_url + "/jobinfo/start";
			needFresh = true;
		} else if ("job_del" == type) {
			typeName = I18n.system_opt_del ;
			url = base_url + "/jobinfo/remove";
			needFresh = true;
		} else {
			return;
		}

		var id = $(this).parents('ul').attr("_id");

		layer.confirm( I18n.system_ok + typeName + '?', {
			icon: 3,
			title: I18n.system_tips ,
			btn: [ I18n.system_ok, I18n.system_cancel ]
		}, function(index){
			layer.close(index);

			$.ajax({
				type : 'POST',
				url : url,
				data : {
					"id" : id
				},
				dataType : "json",
				success : function(data){
					if (data.code == 200) {
						layer.msg( typeName + I18n.system_success );
						if (needFresh) {
							//window.location.reload();
							jobTable.fnDraw(false);
						}
					} else {
						layer.msg( data.msg || typeName + I18n.system_fail );
					}
				}
			});
		});
	});

	// job trigger
	$("#job_list").on('click', '.job_trigger',function() {
		var id = $(this).parents('ul').attr("_id");
		var row = tableData['key'+id];

		$("#jobTriggerModal .form input[name='id']").val( row.id );
		$("#jobTriggerModal .form textarea[name='executorParam']").val( row.executorParam );

		$('#jobTriggerModal').modal({backdrop: false, keyboard: false}).modal('show');
	});
	$("#jobTriggerModal .ok").on('click',function() {
		$.ajax({
			type : 'POST',
			url : base_url + "/jobinfo/trigger",
			data : {
				"id" : $("#jobTriggerModal .form input[name='id']").val(),
				"executorParam" : $("#jobTriggerModal .textarea[name='executorParam']").val(),
				"addressList" : $("#jobTriggerModal .textarea[name='addressList']").val()
			},
			dataType : "json",
			success : function(data){
				if (data.code == 200) {
					$('#jobTriggerModal').modal('hide');

					layer.msg( I18n.jobinfo_opt_run + I18n.system_success );
				} else {
					layer.msg( data.msg || I18n.jobinfo_opt_run + I18n.system_fail );
				}
			}
		});
	});
	$("#jobTriggerModal").on('hide.bs.modal', function () {
		$("#jobTriggerModal .form")[0].reset();
	});


	// job registryinfo
	$("#job_list").on('click', '.job_registryinfo',function() {
		var id = $(this).parents('ul').attr("_id");
		var row = tableData['key'+id];

		var jobGroup = row.jobGroup;

		$.ajax({
			type : 'POST',
			url : base_url + "/jobgroup/loadById",
			data : {
				"id" : jobGroup
			},
			dataType : "json",
			success : function(data){

				var html = '<div>';
				if (data.code == 200 && data.content.registryList) {
					for (var index in data.content.registryList) {
						html += (parseInt(index)+1) + '. <span class="badge bg-green" >' + data.content.registryList[index] + '</span><br>';
					}
				}
				html += '</div>';

				layer.open({
					title: I18n.jobinfo_opt_registryinfo ,
					btn: [ I18n.system_ok ],
					content: html
				});

			}
		});

	});

	// job_next_time
	$("#job_list").on('click', '.job_next_time',function() {
		var id = $(this).parents('ul').attr("_id");
		var row = tableData['key'+id];

		$.ajax({
			type : 'POST',
			url : base_url + "/jobinfo/nextTriggerTime",
			data : {
				"scheduleType" : row.scheduleType,
				"scheduleConf" : row.scheduleConf
			},
			dataType : "json",
			success : function(data){

				if (data.code != 200) {
					layer.open({
						title: I18n.jobinfo_opt_next_time ,
						btn: [ I18n.system_ok ],
						content: data.msg
					});
				} else {
					var html = '<center>';
					if (data.code == 200 && data.content) {
						for (var index in data.content) {
							html += '<span>' + data.content[index] + '</span><br>';
						}
					}
					html += '</center>';

					layer.open({
						title: I18n.jobinfo_opt_next_time ,
						btn: [ I18n.system_ok ],
						content: html
					});
				}

			}
		});

	});

	// add
	$(".add").click(function(){

		// init-cronGen
		$("#addModal .form input[name='schedule_conf_CRON']").show().siblings().remove();
		$("#addModal .form input[name='schedule_conf_CRON']").cronGen({});

		// 》init scheduleType
		$("#updateModal .form select[name=scheduleType]").change();

		// 》init glueType
		$("#updateModal .form select[name=glueType]").change();

		//init sourceType
		$("#updateModal .form select[name=sourceType]").change();


		$('#addModal').modal({backdrop: false, keyboard: false}).modal('show');
	});
	let $dataRoute = 2;
	var addModalValidate = $("#addModal .form").validate({
		errorElement : 'span',
		errorClass : 'help-block',
		focusInvalid : true,
		rules : {
			jobDesc : {
				required : true,
				maxlength: 50
			},
			author : {
				required : true
			}/*,
            executorTimeout : {
                digits:true
            },
            executorFailRetryCount : {
                digits:true
            }*/
		},
		messages : {
			jobDesc : {
				required : I18n.system_please_input + I18n.jobinfo_field_jobdesc
			},
			author : {
				required : I18n.system_please_input + I18n.jobinfo_field_author
			}/*,
            executorTimeout : {
                digits: I18n.system_please_input + I18n.system_digits
            },
            executorFailRetryCount : {
                digits: I18n.system_please_input + I18n.system_digits
            }*/
		},
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
		},
		success : function(label) {
			label.closest('.form-group').removeClass('has-error');
			label.remove();
		},
		errorPlacement : function(error, element) {
			element.parent('div').append(error);
		},
		submitHandler : function(form) {

			// process executorTimeout+executorFailRetryCount
			var executorTimeout = $("#addModal .form input[name='executorTimeout']").val();
			if(!/^\d+$/.test(executorTimeout)) {
				executorTimeout = 0;
			}
			$("#addModal .form input[name='executorTimeout']").val(executorTimeout);
			var executorFailRetryCount = $("#addModal .form input[name='executorFailRetryCount']").val();
			if(!/^\d+$/.test(executorFailRetryCount)) {
				executorFailRetryCount = 0;
			}
			$("#addModal .form input[name='executorFailRetryCount']").val(executorFailRetryCount);

			// process schedule_conf
			var scheduleType = $("#addModal .form select[name='scheduleType']").val();
			var scheduleConf;
			if (scheduleType == 'CRON') {
				scheduleConf = $("#addModal .form input[name='cronGen_display']").val();
			} else if (scheduleType == 'FIX_RATE') {
				scheduleConf = $("#addModal .form input[name='schedule_conf_FIX_RATE']").val();
			} else if (scheduleType == 'FIX_DELAY') {
				scheduleConf = $("#addModal .form input[name='schedule_conf_FIX_DELAY']").val();
			}
			$("#addModal .form input[name='scheduleConf']").val( scheduleConf );

			//let $("#addModal
			$.post(base_url + "/jobinfo/add",  $("#addModal .form").serialize(), function(data, status) {
				if (data.code == "200") {
					$('#addModal').modal('hide');
					layer.open({
						title: I18n.system_tips ,
						btn: [ I18n.system_ok ],
						content: I18n.system_add_suc ,
						icon: '1',
						end: function(layero, index){
							jobTable.fnDraw();
							//window.location.reload();
						}
					});
				} else {
					layer.open({
						title: I18n.system_tips ,
						btn: [ I18n.system_ok ],
						content: (data.msg || I18n.system_add_fail),
						icon: '2'
					});
				}
			});
		}
	});
	$("#addModal").on('hide.bs.modal', function () {
		addModalValidate.resetForm();
		$("#addModal .form")[0].reset();
		$("#addModal .form .form-group").removeClass("has-error");
		$(".remote_panel").show();	// remote

		$("#addModal .form input[name='executorHandler']").removeAttr("readonly");
	});

	// jobType change
	$("#addModal input[name=jobType], #updateModal input[name=jobType]").click(function(){
		var jobType = $(this).val();
		var select_name = document.getElementsByTagName("select");
		var $jobGroup = $(this).parents("form").find("select[name=jobGroup]");
		if (jobType == 0) {
			$jobGroup.attr("disabled","disabled");

		} else {

			$jobGroup.removeAttr("disabled");
		}
	});

	// scheduleType change
	$(".scheduleType").change(function(){
		var scheduleType = $(this).val();
		$(this).parents("form").find(".schedule_conf").hide();
		$(this).parents("form").find(".schedule_conf_" + scheduleType).show();

	});

	// glueType change
	// sourceType
	$(".sourceType").change(function(){
		// executorHandler
		//var $dataRoute = $(this).parents("form").find("input[name='dataRoute']");
		var $cpu = $(this).parents("form").find("input[name='cpu']");
		var $memory = $(this).parents("form").find("input[name='memory']");
		//var checkbox = $(this).parents("form").find("input[name='checkbox']");
		//var $sourceType = document.getElementsByName('checkbox');
		var str = '';
		// for(var i=0;i<checkbox.length;i++){
		// 	if(checkbox[i].checked){
		// 		str += checkbox[i].value + ',';
		// 	}
		// }
		var $sourceType = $(this).parents("form").find("input[name='sourceType']");
		//var $sourceType = str;
		//console.log($memory);

	});

	//上传按钮
	// $("#upload").on('click', function() {
	// 	let name = $("#fileUpload").val();
	// 	let fileName = $("#fileUpload")[0].files[0];
	// 	console.log("name" + name);
	// 	console.log(fileName);
	// 	if (fileName == null) {
	// 		$("#console").html("请先选择正确格式的文件！");
	// 	} else {
	// 		uploadoutpersonhot();
	// 	}
	// });




	// update
	$("#job_list").on('click', '.update',function() {

		var id = $(this).parents('ul').attr("_id");
		var row = tableData['key'+id];

		// fill base
		$("#updateModal .form input[name='id']").val( row.id );
		$('#updateModal .form select[name=jobGroup] option[value='+ row.jobGroup +']').prop('selected', true);
		$("#updateModal .form input[name='jobDesc']").val( row.jobDesc );
		$("#updateModal .form input[name='author']").val( row.author );
		$("#updateModal .form input[name='alarmEmail']").val( row.alarmEmail );

		// fill trigger
		$('#updateModal .form select[name=scheduleType] option[value='+ row.scheduleType +']').prop('selected', true);
		$("#updateModal .form input[name='scheduleConf']").val( row.scheduleConf );
		if (row.scheduleType == 'CRON') {
			$("#updateModal .form input[name='schedule_conf_CRON']").val( row.scheduleConf );
		} else if (row.scheduleType == 'FIX_RATE') {
			$("#updateModal .form input[name='schedule_conf_FIX_RATE']").val( row.scheduleConf );
		} else if (row.scheduleType == 'FIX_DELAY') {
			$("#updateModal .form input[name='schedule_conf_FIX_DELAY']").val( row.scheduleConf );
		}

		// 》init scheduleType
		$("#updateModal .form select[name=scheduleType]").change();

		// fill job
		$('#updateModal .form select[name=glueType] option[value='+ row.glueType +']').prop('selected', true);
		$("#updateModal .form input[name='executorHandler']").val( row.executorHandler );
		$("#updateModal .form textarea[name='executorParam']").val( row.executorParam );

		// 》init sourceType
		$("#updateModal .form select[name=sourceType]").change();

		// 》init-cronGen
		$("#updateModal .form input[name='schedule_conf_CRON']").show().siblings().remove();
		$("#updateModal .form input[name='schedule_conf_CRON']").cronGen({});

		// fill advanced
		$('#updateModal .form select[name=executorRouteStrategy] option[value='+ row.executorRouteStrategy +']').prop('selected', true);
		$("#updateModal .form input[name='childJobId']").val( row.childJobId );
		$('#updateModal .form select[name=misfireStrategy] option[value='+ row.misfireStrategy +']').prop('selected', true);
		$('#updateModal .form select[name=executorBlockStrategy] option[value='+ row.executorBlockStrategy +']').prop('selected', true);
		$("#updateModal .form input[name='executorTimeout']").val( row.executorTimeout );
		$("#updateModal .form input[name='executorFailRetryCount']").val( row.executorFailRetryCount );

		// show
		$('#updateModal').modal({backdrop: false, keyboard: false}).modal('show');
	});
	var updateModalValidate = $("#updateModal .form").validate({
		errorElement : 'span',
		errorClass : 'help-block',
		focusInvalid : true,

		rules : {
			jobDesc : {
				required : true,
				maxlength: 50
			},
			author : {
				required : true
			}
		},
		messages : {
			jobDesc : {
				required : I18n.system_please_input + I18n.jobinfo_field_jobdesc
			},
			author : {
				required : I18n.system_please_input + I18n.jobinfo_field_author
			}
		},
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
		},
		success : function(label) {
			label.closest('.form-group').removeClass('has-error');
			label.remove();
		},
		errorPlacement : function(error, element) {
			element.parent('div').append(error);
		},
		submitHandler : function(form) {

			// process executorTimeout + executorFailRetryCount
			var executorTimeout = $("#updateModal .form input[name='executorTimeout']").val();
			if(!/^\d+$/.test(executorTimeout)) {
				executorTimeout = 0;
			}
			$("#updateModal .form input[name='executorTimeout']").val(executorTimeout);
			var executorFailRetryCount = $("#updateModal .form input[name='executorFailRetryCount']").val();
			if(!/^\d+$/.test(executorFailRetryCount)) {
				executorFailRetryCount = 0;
			}
			$("#updateModal .form input[name='executorFailRetryCount']").val(executorFailRetryCount);


			// process schedule_conf
			var scheduleType = $("#updateModal .form select[name='scheduleType']").val();
			var scheduleConf;
			if (scheduleType == 'CRON') {
				scheduleConf = $("#updateModal .form input[name='cronGen_display']").val();
			} else if (scheduleType == 'FIX_RATE') {
				scheduleConf = $("#updateModal .form input[name='schedule_conf_FIX_RATE']").val();
			} else if (scheduleType == 'FIX_DELAY') {
				scheduleConf = $("#updateModal .form input[name='schedule_conf_FIX_DELAY']").val();
			}
			$("#updateModal .form input[name='scheduleConf']").val( scheduleConf );

			// post
			$.post(base_url + "/jobinfo/update", $("#updateModal .form").serialize(), function(data, status) {
				if (data.code == "200") {
					$('#updateModal').modal('hide');
					layer.open({
						title: I18n.system_tips ,
						btn: [ I18n.system_ok ],
						content: I18n.system_update_suc ,
						icon: '1',
						end: function(layero, index){
							//window.location.reload();
							jobTable.fnDraw();
						}
					});
				} else {
					layer.open({
						title: I18n.system_tips ,
						btn: [ I18n.system_ok ],
						content: (data.msg || I18n.system_update_fail ),
						icon: '2'
					});
				}
			});
		}
	});
	$("#updateModal").on('hide.bs.modal', function () {
		updateModalValidate.resetForm();
		$("#updateModal .form")[0].reset();
		$("#updateModal .form .form-group").removeClass("has-error");
	});

	/**
	 * find title by name, GlueType
	 */
	function findGlueTypeTitle(glueType) {
		var glueTypeTitle;
		$("#addModal .form select[name=glueType] option").each(function () {
			var name = $(this).val();
			var title = $(this).text();
			if (glueType == name) {
				glueTypeTitle = title;
				return false
			}
		});
		return glueTypeTitle;
	}

	// job_copy
	$("#job_list").on('click', '.job_copy',function() {

		var id = $(this).parents('ul').attr("_id");
		var row = tableData['key'+id];

		// fill base
		$('#addModal .form select[name=jobGroup] option[value='+ row.jobGroup +']').prop('selected', true);
		$("#addModal .form input[name='jobDesc']").val( row.jobDesc );
		$("#addModal .form input[name='author']").val( row.author );
		$("#addModal .form input[name='alarmEmail']").val( row.alarmEmail );

		// fill trigger
		$('#addModal .form select[name=scheduleType] option[value='+ row.scheduleType +']').prop('selected', true);
		$("#addModal .form input[name='scheduleConf']").val( row.scheduleConf );
		if (row.scheduleType == 'CRON') {
			$("#addModal .form input[name='schedule_conf_CRON']").val( row.scheduleConf );
		} else if (row.scheduleType == 'FIX_RATE') {
			$("#addModal .form input[name='schedule_conf_FIX_RATE']").val( row.scheduleConf );
		} else if (row.scheduleType == 'FIX_DELAY') {
			$("#addModal .form input[name='schedule_conf_FIX_DELAY']").val( row.scheduleConf );
		}

		// 》init scheduleType
		$("#addModal .form select[name=scheduleType]").change();

		// fill job
		$('#addModal .form select[name=glueType] option[value='+ row.glueType +']').prop('selected', true);
		$("#addModal .form input[name='executorHandler']").val( row.executorHandler );
		$("#addModal .form textarea[name='executorParam']").val( row.executorParam );

		// 》init glueType
		$("#addModal .form select[name=glueType]").change();

		// 》init-cronGen
		$("#addModal .form input[name='schedule_conf_CRON']").show().siblings().remove();
		$("#addModal .form input[name='schedule_conf_CRON']").cronGen({});

		// fill advanced
		$('#addModal .form select[name=executorRouteStrategy] option[value='+ row.executorRouteStrategy +']').prop('selected', true);
		$("#addModal .form input[name='childJobId']").val( row.childJobId );
		$('#addModal .form select[name=misfireStrategy] option[value='+ row.misfireStrategy +']').prop('selected', true);
		$('#addModal .form select[name=executorBlockStrategy] option[value='+ row.executorBlockStrategy +']').prop('selected', true);
		$("#addModal .form input[name='executorTimeout']").val( row.executorTimeout );
		$("#addModal .form input[name='executorFailRetryCount']").val( row.executorFailRetryCount );

		// show
		$('#addModal').modal({backdrop: false, keyboard: false}).modal('show');
	});



});
